package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourcesController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 查询资源列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> findResourceList(@RequestBody ResourceDto dto){
        List<ResourceVo> voList = resourceService.findResourceList(dto);
        return ResponseResult.success(voList);
    }

    /**
     * 资源的树形结构
     * @param itemVo
     * @return
     */
    @PostMapping("/tree")
    public ResponseResult<TreeVo> findResourceTree(@RequestBody TreeItemVo itemVo){
        TreeVo treeVo = resourceService.findResourceTree(itemVo);
        return ResponseResult.success(treeVo);
    }

    /**
     * 添加资源
     * @param dto
     * @return
     */
    @PutMapping
    public ResponseResult addResource(@RequestBody ResourceDto dto){
//        对象属性拷贝
        Resource resource = BeanUtil.toBean(dto, Resource.class);
        resourceService.addResource(resource);
        return ResponseResult.success();
    }

    /**
     * 后台管理登录后,获取当前用户的菜单数据
     * @return
     */
    @GetMapping("/menus")
    public ResponseResult<List<MenuVo>> menus(){
        List<MenuVo> menuVoList = resourceService.menus();
        return ResponseResult.success(menuVoList);
    }
}
